package 高频题;

public class 回文子串_647 {

    int res = 0;

    public static void main(String[] args) {
        System.out.println(new 回文子串_647().countSubstrings("abcc"));
    }

    public int countSubstrings(String s) {
        for (int i = 0; i < s.length(); i++) {
            //寻找长度为奇数的回文子串
            res = res + palindrome(s, i, i);
            //寻找长度为偶数的回文子串
            res = res + palindrome(s, i, i + 1);
        }

        return res;
    }


    //返回以s[l]和s[r]为中心的最长回文子串
    public int palindrome(String s, int l, int r) {
        int curRes = 0;
        while (l >= 0 && r < s.length() && s.charAt(l) == s.charAt(r)) {
            //从中间向两端扩散
            l--;
            r++;
            //是回文串，那么长度就加一
            curRes++;
        }

        return curRes;

    }
}
